package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
@TableBind(tableName="device_activation",pkName = "id")
public class DeviceActivation extends BasicModel<DeviceActivation> {
    public static final DeviceActivation dao = new DeviceActivation();
    public static final String SELECT_ALL = "`id`,`device_id`,`user_id`,`sn`,`begin_time`,`end_time`,`warranty_time`,`status`,`memo`";

    public Page<DeviceActivation> paginate(Integer pageNumber, Integer pageSize, String select, DeviceActivation model) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM device_activation da, user u WHERE da.user_id = u.user_id");
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(select);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (model != null) {
            String[] attrNames = model.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = model.get(attrName);
                if (attrValue != null) {
                    sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                    paras.add(attrValue);
                }
            }
        }

        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }

    /**
     * 查询在某时间之后的激活记录
     */
    public DeviceActivation findByBeginTime(String sn, Date startTime, Date endTime) {
        return findFirst("SELECT * FROM device_activation WHERE sn = ? AND begin_time >= ? AND begin_time <= ?", sn, startTime, endTime);
    }

    public DeviceActivation findByDevice(Long deviceId) {
        return findFirst("SELECT * FROM device_activation WHERE device_id = ?", deviceId);
    }

    public List<DeviceActivation> findByUser(Long userId) {
        return find("SELECT * FROM device_activation WHERE user_id = ?", userId);
    }

    public DeviceActivation findBySnAndStatus(String sn, Integer status) {
        return findFirst("SELECT * FROM device_activation WHERE sn = ? AND status = ?", sn, status);
    }

    public DeviceActivation findBySn(String sn) {
        return findFirst("SELECT * FROM device_activation WHERE sn = ?", sn);
    }

    public DeviceActivation findByDeviceOrSn(Long deviceId, String sn) {
        return findFirst("SELECT * FROM device_activation WHERE device_id = ? OR sn = ?", deviceId, sn);
    }
            
    public DeviceActivation setId(Long id){
        return this.set("id",id);
    }

    public Long getId(){
        return this.get("id");
    }
        
    public DeviceActivation setDeviceId(Long deviceId){
        return this.set("device_id",deviceId);
    }

    public Long getDeviceId(){
        return this.get("device_id");
    }

    public Device getDevice(){
        return Device.dao.findById(getDeviceId());
    }
        
    public DeviceActivation setUserId(Long userId){
        return this.set("user_id",userId);
    }

    public Long getUserId(){
        return this.get("user_id");
    }

    public User getUser(){
        return User.dao.findById(getUserId());
    }
        
    public DeviceActivation setSn(String sn){
        return this.set("sn",sn);
    }

    public String getSn(){
        return this.getStr("sn");
    }
        
    public DeviceActivation setBeginTime(Date beginTime){
        return this.set("begin_time",beginTime);
    }

    public Date getBeginTime(){
        return this.get("begin_time");
    }
        
    public DeviceActivation setEndTime(Date endTime){
        return this.set("end_time",endTime);
    }

    public Date getEndTime(){
        return this.get("end_time");
    }
        
    public DeviceActivation setWarrantyTime(Date warrantyTime){
        return this.set("warranty_time",warrantyTime);
    }

    public Date getWarrantyTime(){
        return this.get("warranty_time");
    }
        
    public DeviceActivation setStatus(Integer status){
        return this.set("status",status);
    }

    public Integer getStatus(){
        return this.get("status");
    }
        
    public DeviceActivation setMemo(String memo){
        return this.set("memo",memo);
    }

    public String getMemo(){
        return this.getStr("memo");
    }

}
